<?php
namespace app\system\controller;

use app\common\model\PatchModel;
use think\Request;

class PatchController extends BaseController
{

    private $patchModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->patchModel = new PatchModel();
    }

    /**
     * 碎片首页
     * @return mixed
     */
    public function index()
    {
        $patchList = $this->patchModel->paginate(12, false);
        $this->assign('patchList', $patchList);
        return $this->fetch();
    }

    /**
     * 添加碎片
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            try{
                $data                  = $this->request->param();
                $data['patch_content'] = $this->patchModel->formatPatch($data);
                $result                = $this->validate($data, [
                    'patch_title|碎片标题'   => 'require',
                    'patch_sn|碎片标识'   => 'require',
                    'patch_type|碎片类型'    => 'require|number',
                    'patch_content|碎片内容' => 'require',
                ]);
                if ($result !== true) {
                    exception($result,40001);
                }
                $info = $this->patchModel->where('patch_sn',$data['patch_sn'])->find();
                if($info){
                    exception('该碎片标识已存在！',40001);
                }
                $this->patchModel->addData($data);
            } catch (\Exception $e){
                $this->success('添加成功！', url('index'));
            }

        }
        $typeList = get_dict('102');
        $this->assign('utype',$this->getUtype());
        $this->assign('typeList', $typeList);
        return $this->fetch();
    }


    /**
     * 修改碎片
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            try{
                $data                  = $this->request->param();
                $data['patch_content'] = $this->patchModel->formatPatch($data);
                $result                = $this->validate($data, [
                    'patch_title|碎片标题'   => 'require',
                    'patch_sn|碎片标识'   => 'require',
                    'patch_type|碎片类型'    => 'require|number',
                    'patch_content|碎片内容' => 'require',
                ]);
                if ($result !== true) {
                    exception($result,40001);
                }
                $info = $this->patchModel->where('patch_id',$data['patch_id'])->find();
                if($info['patch_sn']!=$data['patch_sn']){
                    $info = $this->patchModel->where('patch_sn',$data['patch_sn'])->find();
                    if($info){
                        exception('该碎片标识已存在',40001);
                    }
                }
                $this->patchModel->editData($data);
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('修改成功', url('index'));
        }
        $typeList = get_dict('102');
        $patchId  = $this->request->param('patch_id/d');
        $info     = $this->patchModel->get($patchId);
        $this->assign('utype',$this->getUtype());
        $this->assign('typeList', $typeList);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del()
    {
        try{
            $patchId = $this->request->param('patch_id');
            $this->patchModel->delData($patchId);
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }
}
